//
//  SYBaseViewController.m
//  ChatDemo-UI2.0
//
//  Created by syun on 16/7/4.
//  Copyright © 2016年 syun. All rights reserved.
//

#import "SYBaseViewController.h"
#import "FRNavigationItemButton.h"



@interface SYBaseViewController ()

@end

@implementation SYBaseViewController

- (SYNavigationController *)syNavController
{
    return (SYNavigationController *)self.navigationController;
}

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.navBar = [[SYNavigationBar alloc] initWithFrame:CGRectMake(0, 0, SCREENWIDTH, SafeAreaTopHeight)];
        [self setUp];
        
    }
    return self;
}

- (void)awakeFromNib {
    
    [super awakeFromNib];
    [self setUp];
    
}


- (id)init
{
    self = [super init];
    if (self) {
        self.navBar = [[SYNavigationBar alloc] initWithFrame:CGRectMake(0, 0, SCREENWIDTH, SafeAreaTopHeight)];
        [self setUp];
    }
    return self;
}

- (void)setUp
{
    if (iOS7_or_Later) {
        self.automaticallyAdjustsScrollViewInsets = NO;
        self.navigationController.navigationBar.translucent = NO;
        self.edgesForExtendedLayout = UIRectEdgeNone;
    }else {
        
    }
    
//    _left = [[UIBarButtonItem alloc] initWithImage:[UIImage imageWithOriginalName:@"back"] style:UIBarButtonItemStyleDone target:self action:@selector(back)];
//    self.navigationItem.leftBarButtonItem = _left;
    self.hidesBottomBarWhenPushed = YES;
    
}

- (void)loadView
{
    [super loadView];
    
    self.view.backgroundColor = colorFromHex(@"f4f5f7");
    self.view.autoresizesSubviews = YES;
    
}

-(void)viewWillAppear:(BOOL)animated{
    
    [super viewWillAppear:animated];
    
    if (_isRootViewController) {
        
        self.navBar.backImg.hidden = YES;
        self.navBar.backButton.hidden = YES;
    }
    
}

- (void)viewDidAppear:(BOOL)animated {
    
    [super viewDidAppear:animated];
    
    if (self.navigationController.childViewControllers.count == 1) {
        if ([self.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
            self.navigationController.interactivePopGestureRecognizer.enabled = NO;
        }
    }
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //_navBar.backgroundColor = colorFromHex(@"#3bc144");
    
    self.navigationController.navigationBar.hidden = YES;
    _navBar.hidden = NO;
    [self.view addSubview:_navBar];
    
    [_navBar.backButton addTarget:self action:@selector(backButtonClick) forControlEvents:UIControlEventTouchUpInside];
    
    self.navigationController.interactivePopGestureRecognizer.delegate = (id)self;
    
    if ([[[UIDevice currentDevice] systemVersion] floatValue]>=8.0) {
        self.modalPresentationStyle=UIModalPresentationOverCurrentContext;
    }
    
//    if (!_isRootViewController) {
//        [self addLeftBarButtonItemWithTitle:nil target:self action:@selector(back) backgroundImage:[UIImage imageNamed:@"back"]];
//    }else
//    {
//        _titleLable = [[UILabel alloc]initWithFrame:CGRectMake((SCREENWIDTH - 150)/2, SafeAreaTopHeight - 25-10, 150, 25)];
//        _titleLable.font = [UIFont systemFontOfSize:16];
//        _titleLable.textAlignment = NSTextAlignmentCenter;
//        _titleLable.textColor = [UIColor blackColor];
//        [self.navBar addSubview:_titleLable];
//    }
    
}

//-(UIStatusBarStyle)preferredStatusBarStyle
//{
//    return UIStatusBarStyleLightContent;
//}

- (void)backButtonClick {
    
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)didReceiveMemoryWarning {
    
    [super didReceiveMemoryWarning];//即使没有显示在window上，也不会自动的将self.view释放。
    // Add code to clean up any of your own resources that are no longer necessary.
    
    // 此处做兼容处理需要加上ios6.0的宏开关，保证是在6.0下使用的,6.0以前屏蔽以下代码，否则会在下面使用self.view时自动加载viewDidLoad
    if ([self.view window] == nil)// 是否是正在使用的视图
    {
        // Add code to preserve data stored in the views that might be
        // needed later.
        
        // Add code to clean up other strong references to the view in
        // the view hierarchy.
        self.view = nil;// 目的是再次进入时能够重新加载调用viewDidLoad函数。
    }
    // Dispose of any resources that can be recreated.
}

- (void)addLeftBarButtonItemWithTitle:(NSString *)title target:(id)target action:(SEL)action backgroundImage:(UIImage *)bgImage
{
    
    
    
    CGFloat width = bgImage ? (bgImage.size.width / bgImage.size.height * 25) : 30;
    FRNavigationItemButton *leftButton = [FRNavigationItemButton buttonWithType:UIButtonTypeCustom];
//    [leftButton setFrame:CGRectMake(0, 0, width, 25)];
    [leftButton setFrame:CGRectMake(0, SafeAreaTopHeight - 25-10, 44, 25)];
    [leftButton setTitle:title forState:UIControlStateNormal];
    [leftButton.titleLabel setFont:[UIFont systemFontOfSize:16]];
    //[leftButton setTitleColor:VineBlueColor forState:UIControlStateNormal];
    [leftButton setTitleColor:[UIColor lightGrayColor] forState:UIControlStateDisabled];
    [leftButton setContentHorizontalAlignment:UIControlContentHorizontalAlignmentLeft];
    [leftButton setImage:bgImage forState:UIControlStateNormal];
    [leftButton addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
    if (!bgImage) {
        [leftButton sizeToFit];
    }
    
    UIBarButtonItem *leftBarButton = [[UIBarButtonItem alloc] initWithCustomView:leftButton];
    self.navBar.navigationItem.leftBarButtonItem = leftBarButton;

}



#pragma mark - Actions

- (void)backButtonTapped:(id)sender
{
    [self.navigationController popViewControllerAnimated:YES];
}

@end
